package com.ruoyi.lifecycle.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.lifecycle.domain.bo.TFundFundmanagerMilestoneBo;
import com.ruoyi.lifecycle.domain.vo.TFundFundmanagerMilestoneVo;
import com.ruoyi.lifecycle.domain.TFundFundmanagerMilestone;
import com.ruoyi.lifecycle.mapper.TFundFundmanagerMilestoneMapper;
import com.ruoyi.lifecycle.service.ITFundFundmanagerMilestoneService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 基金-基金经理里程碑Service业务层处理
 *
 * @author chq
 * @date 2025-06-09
 */
@RequiredArgsConstructor
@Service
public class TFundFundmanagerMilestoneServiceImpl implements ITFundFundmanagerMilestoneService {

    private final TFundFundmanagerMilestoneMapper baseMapper;

    /**
     * 查询基金-基金经理里程碑
     */
    @Override
    public TFundFundmanagerMilestoneVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询基金-基金经理里程碑列表
     */
    @Override
    public TableDataInfo<TFundFundmanagerMilestoneVo> queryPageList(TFundFundmanagerMilestoneBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TFundFundmanagerMilestone> lqw = buildQueryWrapper(bo);
        Page<TFundFundmanagerMilestoneVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询基金-基金经理里程碑列表
     */
    @Override
    public List<TFundFundmanagerMilestoneVo> queryList(TFundFundmanagerMilestoneBo bo) {
        LambdaQueryWrapper<TFundFundmanagerMilestone> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TFundFundmanagerMilestone> buildQueryWrapper(TFundFundmanagerMilestoneBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TFundFundmanagerMilestone> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getFundId()), TFundFundmanagerMilestone::getFundId, bo.getFundId());
        lqw.eq(StringUtils.isNotBlank(bo.getFundmanagerId()), TFundFundmanagerMilestone::getFundmanagerId, bo.getFundmanagerId());
        lqw.eq(bo.getDate() != null, TFundFundmanagerMilestone::getDate, bo.getDate());
        lqw.eq(StringUtils.isNotBlank(bo.getMilestone()), TFundFundmanagerMilestone::getMilestone, bo.getMilestone());
        lqw.eq(StringUtils.isNotBlank(bo.getMemo()), TFundFundmanagerMilestone::getMemo, bo.getMemo());
        return lqw;
    }

    /**
     * 新增基金-基金经理里程碑
     */
    @Override
    public Boolean insertByBo(TFundFundmanagerMilestoneBo bo) {
        TFundFundmanagerMilestone add = BeanUtil.toBean(bo, TFundFundmanagerMilestone.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改基金-基金经理里程碑
     */
    @Override
    public Boolean updateByBo(TFundFundmanagerMilestoneBo bo) {
        TFundFundmanagerMilestone update = BeanUtil.toBean(bo, TFundFundmanagerMilestone.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TFundFundmanagerMilestone entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除基金-基金经理里程碑
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
